/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-24 09:42:14  
 * Copyright (c) 2015- joysuch-版权所有     
 */
package com.joysuch.wwyt.bp.service.impl;

import com.joysuch.wwyt.bp.bean.BpTaskListDetailBean;
import com.joysuch.wwyt.bp.bean.BpTaskListEditBean;
import com.joysuch.wwyt.bp.bean.BpTaskListListBean;
import com.joysuch.wwyt.bp.entity.BpTaskList;
import com.joysuch.wwyt.bp.entity.dsl.QBpTaskList;
import com.joysuch.wwyt.bp.repository.BpTaskListDao;
import com.joysuch.wwyt.bp.service.BpTaskListService;
import com.joysuch.wwyt.core.exception.BusinessRestrictionsException;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

import static com.joysuch.wwyt.bp.bean.BpTaskListListBean.Search;

@Service
@Transactional
public class BpTaskListServiceImpl implements BpTaskListService {

    @Autowired
    private BpTaskListDao dao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;

    @Override
    public BpTaskList add(BpTaskListEditBean entity) throws Exception {
        BpTaskList content  = new BpTaskList();
        //用户ID
        content.setUserId(entity.getUserId());
        //筛选SQL
        content.setFilterSql(entity.getFilterSql());
        //模块
        content.setModule(entity.getModule());
        //数据ID
        content.setDataId(entity.getDataId());
        //明细
        content.setDetail(entity.getDetail());
        //状态
        content.setState(entity.getState());
        //处理时间
        content.setHandleTime(entity.getHandleTime());
        //处理备注
        content.setHandleRemark(entity.getHandleRemark());
        //处理人
        content.setHandleUserId(entity.getHandleUserId());
        return dao.save(content);
    }

    @Override
    public void update(BpTaskListEditBean entity) throws Exception {
        BpTaskList content = dao.findById(entity.getId()).get();
        //ID
        content.setId(entity.getId());
        //用户ID
        content.setUserId(entity.getUserId());
        //筛选SQL
        content.setFilterSql(entity.getFilterSql());
        //模块
        content.setModule(entity.getModule());
        //数据ID
        content.setDataId(entity.getDataId());
        //明细
        content.setDetail(entity.getDetail());
        //状态
        content.setState(entity.getState());
        //处理时间
        content.setHandleTime(entity.getHandleTime());
        //处理备注
        content.setHandleRemark(entity.getHandleRemark());
        //处理人
        content.setHandleUserId(entity.getHandleUserId());
        dao.save(content);
    }

    @Override
    public Page<BpTaskListListBean> findByPage(Pageable page , Search condition) {
        QBpTaskList bpTaskList = QBpTaskList.bpTaskList;
        Predicate predicate = bpTaskList.isNotNull();
        Page<BpTaskList> all = dao.findAll(predicate, page);
        ArrayList<BpTaskListListBean> list = new ArrayList<>();
        for (BpTaskList f : all) {
            if(f.getState() !=null &&f.getState() !=2){
                    BpTaskListListBean bean = new BpTaskListListBean();
                    //ID
                    bean.setId(f.getId());
                    //用户ID
                    bean.setUserId(f.getUserId());
                    //筛选SQL
                    bean.setFilterSql(f.getFilterSql());
                    //模块
                    bean.setModule(f.getModule());
                    //数据ID
                    bean.setDataId(f.getDataId());
                    //明细
                    bean.setDetail(f.getDetail());
                    //状态
                    bean.setState(f.getState());
                    if(f.getState() !=null){
                        if(f.getState() == 0){
                            bean.setStateName("【待审核】");
                        }else if(f.getState() == 1){
                            bean.setStateName("【待验证】");
                        }
                    }
                    //处理时间
                    bean.setHandleTime(f.getHandleTime());
                    //处理备注
                    bean.setHandleRemark(f.getHandleRemark());
                    //处理人
                    bean.setHandleUserId(f.getHandleUserId());
                    list.add(bean);
            }
        }
        return new PageImpl<>(list, page, all.getTotalElements());
    }

    private Predicate  prepareListSearchCondition(Search condition) {
        return null;
    }
    @Override
    public void delete(Long id) throws BusinessRestrictionsException {
        //校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用。不能删除时将抛出业务异常
        checkBussinessRestrictions(id);
        dao.deleteById(id);
    }
    
    /**
     * 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用
     * @param id 要删除的业务ID
    // * @throws BussinessRestrictionsException 抛出的业务异常
     */
    private void checkBussinessRestrictions(Long id) throws BusinessRestrictionsException {
        
    }

    //根据用户ID进行查询
    @Override
    public List<BpTaskList> findById(Long id) {
        QBpTaskList bpTaskList = QBpTaskList.bpTaskList;
        Predicate predicate = bpTaskList.isNotNull();
        predicate = id == null ? predicate : ExpressionUtils.and(predicate, bpTaskList.dataId.eq( id ));
       // predicate = id == null ? predicate : ExpressionUtils.and(predicate, bpTaskList.handleTime.after());//greaterThanOrEqualTo
        Iterable<BpTaskList> all = dao.findAll(predicate);
        ArrayList<BpTaskList> list = new ArrayList<>();
        if(all !=null){
            for (BpTaskList taskList : all) {
                list.add(taskList);
            }
        }
        return list;
    }

    @Override
    public BpTaskList getByIds(Long id) {
        BpTaskList one = dao.getOne(id);
        return one;
    }

    @Override
    public BpTaskListDetailBean getById(Long id) {
        BpTaskList entity = getByIds(id) ;
        BpTaskListDetailBean bean = new BpTaskListDetailBean();
        //ID
        bean.setId(entity.getId());

        //用户ID
        bean.setUserId(entity.getUserId());
        //bean.setUserName(entity.getUserName());

        //筛选SQL
        bean.setFilterSql(entity.getFilterSql());

        //模块
        bean.setModule(entity.getModule());
        
        //数据ID
        bean.setDataId(entity.getDataId());
        //bean.setDataName(entity.getDataName());
        
        //明细
        bean.setDetail(entity.getDetail());
        
        //状态
        bean.setState(entity.getState());
        
        //处理时间
        bean.setHandleTime(entity.getHandleTime());
        
        //处理备注
        bean.setHandleRemark(entity.getHandleRemark());
        
        //处理人
        bean.setHandleUserId(entity.getHandleUserId());
        //bean.setHandleUserName(entity.getHandleUserName());
        
        return bean;
    }

}
